package com.cloud.collection.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cloud.collection.service.CollectionService;
import com.cloud.commonapi.jsonResult.Result;
import com.cloud.commonapi.model.Goods_info;
import com.cloud.commonapi.model.Goods_star;
import com.cloud.commonapi.model.User_info;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * author : 黄永佳
 * create : 2019/9/30 0030 8:51
 * modified :
 * 功能 :
 **/
@RestController
@ApiOperation(value = "物品收藏功能")
@RequestMapping("/collection")
public class CollectionController {
    @Autowired
    CollectionService collectionService;
    @Autowired
    RestTemplate restTemplate;

    @ApiOperation(value = "添加收藏")
    @GetMapping("/{id}")
    public Result addStar(@PathVariable int id, HttpSession session){
        if(session.getAttribute("loginId")==null){
            return Result.build(400,"请登录");
        }

        int accountId=Integer.parseInt(session.getAttribute("loginId").toString());
        if(collectionService.already(id,accountId)){
            return Result.build(300,"该商品已存在于您的收藏夹中");
        }
        Goods_star goods_star=new Goods_star();
        goods_star.setAccountId(accountId);
        goods_star.setGoodsId(id);
        if(collectionService.add(goods_star)>0){
            return Result.build(200,"添加成功");
        }else {
            return Result.build(400,"添加失败");
        }
    }

    @ApiOperation(value = "获取收藏的所有物品")
    @GetMapping("/list")
    public Result listAll(HttpSession session){
        if(session.getAttribute("loginId")==null){
            return Result.build(400,"请登录");
        }
        List<Goods_info> goods_infos=collectionService.findAll(Integer.parseInt(session.getAttribute("loginId").toString()));
        List<User_info> userlist=new ArrayList();
        for(int i=0;i<goods_infos.size();i++){
            JSONObject jSONObject= restTemplate.getForObject("http://USERCENTER/usercontroller/userinfo/users/"+goods_infos.get(i).getAccountId(),JSONObject.class);
            JSONObject  data=jSONObject.getJSONObject("data");
            User_info user_info= JSON.parseObject(data.toString(), User_info.class);
            userlist.add(user_info);
        }
        if(goods_infos!=null){
            HashMap<String,List> hashMap=new HashMap<>();
            hashMap.put("goods_infos",goods_infos);
            hashMap.put("user_info",userlist);
            return Result.buildData(202,"查询成功",hashMap);
        }else {
            return Result.build(200,"查询成功，列表为空");
        }
    }
    @ApiOperation(value = "删除收藏物品(传物品id)")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable int id,HttpSession session){
//        Integer accountId=Integer.parseInt(session.getAttribute("loginId").toString());
        int accountId=1;
        if(collectionService.delete(id,accountId)>0){
            return  Result.build(200,"已移出收藏夹");
        }else{
            return Result.build(300,"移出收藏夹失败");
        }
    }
}
